<?php
/**
 * Tên file mình có thể tự chọn nhưng nên đặc theo têm controller sẽ dễ quản lý
 */

/**
 * Tên class: Example_Models_Dao_Pdo_Mysql_Example(Tenmodule_Models_Dao_Pdo_Mysql_Tenfile)
 */

class Enterprise_Models_Dao_Pdo_Mysql_Info extends Tomato_Model_Dao
    implements Enterprise_Models_Interface_Info
{
    public function convert($entity)
    {
        return new Enterprise_Models_Info($entity);
    }

    public function add($info){
        $this->_conn->insert($this->_prefix . 'enterprise_info',
                            array(
                                'enterprise_id' => $info->enterprise_id,
                                'info_title'    => $info->info_title,
                                'info_string'   => $info->info_string,
                                'info_number'   => $info->info_number,
                                'language'      => $info->language,
                            ));
        return $this->_conn->lastInsertId($this->_prefix . 'enterprise_info');
    }

    public function autoAdd($id){
        $arrayTitle = array(
            '1' => 'Giới thiệu',
            '2' => 'Lịch sử',
            '3' => 'Định hướng',
            '4' => 'Chính sách',
            '5' => 'Quy mô doanh nghiệp'
        );

        $arrayTitleEN = array(
            '1' => 'About us',
            '2' => 'History',
            '3' => 'Development',
            '4' => 'Policies',
            '5' => 'Enterprise scale'
        );
        for($i=1;$i<=count($arrayTitle);$i++){
            $info = new Enterprise_Models_Info(array(
                    'enterprise_id'  => $id,
                    'info_title'     => $arrayTitle[$i],
                    'info_number'    => $i,
                    'language'       => 'vi_VN',
            ));
            $this->add($info);

            $infoEn = new Enterprise_Models_Info(array(
                    'enterprise_id'  => $id,
                    'info_title'     => $arrayTitleEN[$i],
                    'info_number'    => $i,
                    'language'       => 'en_US',
            ));
            $this->add($infoEn);
        }
        return true;
    }

    public function bkgetById($id){
        $rs = $this->_conn
                    ->select()
                    ->from(array('i' => $this->_prefix . 'enterprise_info'))
                    ->where('i.enterprise_id = ?', $id)
                    ->where('i.language = ?', $this->_lang)
                    ->query()
                    ->fetchAll();
        return new Tomato_Model_RecordSet($rs, $this);
    }

    public function bkupdate($info)
    {
        return $this->_conn->update($this->_prefix . 'enterprise_info',
                                array(
                                    'info_title'    => $info->info_title,
                                    'info_string'   => $info->info_string,
                                    'info_number'   => $info->info_number,
                                ),
                                array(
                                    'enterprise_id = ?' => $info->enterprise_id,
                                ));
    }
    public function bkdelete($id){
        /*$this->_conn->delete($this->_prefix . 'enterprise_info',
                            array(
                                'enterprise_id = ?' => $id,
                                'language = ?' => $this->_lang,
                            ));*/
        $sql = "Delete from enterprise_info where `enterprise_id` = {$id} AND `language` = '{$this->_lang}'";
        $this->_conn->query($sql);
    }


    /**================Front-end============================================*/

    public function getById($id){
        $row = $this->_conn
                    ->select()
                    ->from(array('info' => $this->_prefix . 'enterprise_info'))
                    ->where('info.info_id = ?', $id)
                    ->limit(1)
                    ->query()
                    ->fetch();
        return (null == $row) ? null : new Enterprise_Models_Info($row);
    }

    public function getInfos($enterprise_id, $limit)
    {
        $rs = $this->_conn
                    ->select()
                    ->limit($limit)
                    ->order('a.info_number')
                    ->from(array('a' => $this->_prefix . 'enterprise_info'))
                    ->where('a.enterprise_id = ?', $enterprise_id)
                    ->where('a.language = ?', $this->_lang)
                    ->query()
                    ->fetchAll();
        $infos = array();
        if ($rs) {
            foreach ($rs as $row) {
                $infos[] = $row;
            }
        }
        return $infos;
    }

    public function setLang($lang){
        $this->_lang = $lang;
    }

    public function update($info)
    {
        return $this->_conn->update($this->_prefix . 'enterprise_info',
                                array(
                                    'info_title'    => $info->info_title,
                                    'info_string'   => $info->info_string,
                                    'info_number'   => $info->info_number,
                                    'enterprise_id' => $info->enterprise_id,
                                    'language'         => $info->language
                                ),
                                array(
                                    'info_id = ?' => $info->info_id,
                                ));
    }
    public function delete($id){
        /*$this->_conn->delete($this->_prefix . 'enterprise_info',
                            array(
                                'enterprise_id = ?' => $id,
                                'language = ?' => $this->_lang,
                            ));*/
        $sql = "Delete from enterprise_info where `info_id` = {$id} AND `language` = '{$this->_lang}'";
        $this->_conn->query($sql);
    }

    /* ========== For translation =========================================== */

    public function getTranslations($item)
    {
        $rs = $this->_conn
                    ->select()
                    ->from(array('info' => $this->_prefix . 'enterprise_info'))
                    ->joinInner(array('tr' => $this->_prefix . 'core_translation'),
                        'tr.item_class = ?
                        AND (tr.item_id = ? OR tr.source_item_id = ?)
                        AND (tr.item_id = info.info_id OR tr.source_item_id = info.info_id)',
                        array('tr.source_item_id'))
                    ->group('info.info_id')
                    ->bind(array(
                        'Enterprise_Models_Info',
                        $item->info_id,
                        $item->info_id,
                    ))
                    ->query()
                    ->fetchAll();
        return (null == $rs) ? null : new Tomato_Model_RecordSet($rs, $this);
    }

    public function getTranslatable($lang)
    {
        $rs = $this->_conn
                    ->select()
                    ->from(array('info' => $this->_prefix . 'enterprise_info'))
                    ->joinLeft(array('tr' => $this->_prefix . 'core_translation'),
                            'tr.source_item_id = info.info_id
                            AND tr.item_class = ?
                            AND tr.language = ?',
                            array('translatable' => '(tr.item_id IS NULL)'))
                    ->where('info.language = ?', $this->_lang)
                    ->order('info.info_id DESC')
                    ->bind(array(
                        'Enterprise_Models_Info',
                        $lang,
                    ))
                    ->query()
                    ->fetchAll();
        return new Tomato_Model_RecordSet($rs, $this);
    }

    public function getSource($info)
    {
        $row = $this->_conn
                    ->select()
                    ->from(array('info' => $this->_prefix . 'enterprise_info'))
                    ->joinInner(array('tr' => $this->_prefix . 'core_translation'), 'info.info_id = tr.source_item_id', array())
                    ->where('tr.item_id = ?', $info->info_id)
                    ->where('tr.item_class = ?', 'Enterprise_Models_Info')
                    ->limit(1)
                    ->query()
                    ->fetch();
        return (null == $row) ? null : new Enterprise_Models_Info($row);
    }
}
